import com.troyworks.hsmf.Hsmf;
import com.troyworks.hsmf.AEvent;
import com.troyworks.hsmf.Signal;

/**
 * @author Troy Gardner
 */
class com.troyworks.hsmf.TwoStateToggle extends Hsmf {
	public static var SIG_TOGGLE:Signal = Signal.getNext("TOGGLE");
	public static var EVT_TOGGLE:AEvent = new AEvent(SIG_TOGGLE);
	
	public var history:Array = new Array();
	/************************************************
	 *  Constructor 
	 */
	
	function TwoStateToggle(initState : Function, hsmfName : String, aInit : Boolean) {
		super((initState == null)?s_initial:initState, (hsmfName== null)?"TwoStateToggle": hsmfName);
		if(aInit == null || aInit == true){
			init();
		}
	}
	function clearHistory():Void{
		history = new Array();
	}
	function toggle():Void{
		Q_dispatch(EVT_TOGGLE);
	}
	function terminate():Void{
		Q_dispatch(new AEvent(Q_TERMINATE_SIG));
	}
	function restart():Void{
		Q_TRAN(s_initial);
	}
	
	/*..PSEUDOSTATE...............................................................*/
	function s_initial(e : AEvent) : Void
	{
		trace("************************* s_initial " +e.sig.name+" ******************");
		onFunctionEnter ("s_initial-", e, []);
		
		if(e.sig != Q_TRACE_SIG){
			history.push("s_initial-"+e.sig.name);
			Q_INIT(s_OFF);
		}
	}
	/*.................................................................*/
	function s_OFF(e : AEvent) : Function
	{
		trace("************************* s_OFF " + e.sig.name+" ******************");
		onFunctionEnter ("s_OFF-", e, []);
		switch (e.sig)
		{
			case Q_ENTRY_SIG :
			{
				history.push("s_OFF-"+e.sig.name);
				return null;
			}
			case Q_INIT_SIG :
			{
				history.push("s_OFF-"+e.sig.name);
				return null;
			}
			case Q_EXIT_SIG :
			{
				history.push("s_OFF-"+e.sig.name);
				return null;
			}
			
			case SIG_TOGGLE:{
				history.push("s_OFF-"+e.sig.name);
				Q_TRAN(s_ON);
				return null;
			}
			case Q_TERMINATE_SIG:
			{
				history.push("s_OFF-"+e.sig.name);
				Q_TRAN(s_final);
				return null;
			}
		}
		return s_top;
	}	
	
	/*.................................................................*/
	function s_ON(e : AEvent) : Function
	{
		trace("************************* s_ON " +e.sig.name+" ******************");
		onFunctionEnter ("s_ON-", e, []);
		switch (e.sig)
		{
			case Q_ENTRY_SIG :
			{
				history.push("s_ON-"+e.sig.name);
				return null;
			}
			case Q_INIT_SIG :
			{
				history.push("s_ON-"+e.sig.name);
				return null;
			}
			case Q_EXIT_SIG :
			{
				history.push("s_ON-"+e.sig.name);
				return null;
			}
			
			case SIG_TOGGLE:{
				history.push("s_ON-"+e.sig.name);
				Q_TRAN(s_ON);
				return null;
			}
			case Q_TERMINATE_SIG:
			{
				history.push("s_ON-"+e.sig.name);
				Q_TRAN(s_final);
				return null;
			}
		}
		return s_top;
	}
	/*..PSEUDOSTATE...............................................................*/
	function s_final(e : AEvent) : Void
	{
		this.onFunctionEnter ("s_final-", e, []);
		switch (e.sig)
		{
			case Q_ENTRY_SIG :
			{
				history.push("s_final-"+ e.sig.name);
			}
			case Q_INIT_SIG :
			{
				history.push("s_final-"+e.sig.name);
		
			}
			case Q_EXIT_SIG :
			{
				history.push("s_final-"+e.sig.name);
		
			}
		}
	}
}
